<?php
@header('Content-Type: text/html; charset=UTF-8');
class qqlogin {
	public function getvcpic(){
		$uin=empty($_GET['uin'])?exit('{"saveOK":-1,"msg":"uin不能为空"}'):$_GET['uin'];
		$sig=empty($_GET['sig'])?exit('{"saveOK":-1,"msg":"sig不能为空"}'):$_GET['sig'];
		header('content-type:image/jpeg');
		$url='http://captcha.qq.com/getimgbysig?aid=549000929&uin='.$uin.'&sig='.$sig;
		echo $this->get_curl($url);
	}
	public function addsave(){
		$user=$this->login_user;
		$uin=empty($_POST['uin'])?exit('{"saveOK":-1,"msg":"uin不能为空"}'):$_POST['uin'];
		$sid=empty($_POST['sid'])?exit('{"saveOK":-1,"msg":"sid不能为空"}'):$_POST['sid'];
		$skey=empty($_POST['skey'])?exit('{"saveOK":-1,"msg":"skey不能为空"}'):$_POST['skey'];
		if($row=M("qqs")->field('qid')->where("qq='$uin' and uid='$user[uid]'")->find()){
			$data['sid'] = $sid;
			$data['skey'] = $skey;
			$data['qq'] = $uin;
			$data['sidzt'] = 0;
			M("qqs")->where("qid='$row[qid]'")->save($data);
			exit('{"saveOK":0,"msg":"修改成功","url":"'.U('set').'&qid='.$row[qid].'"}');
		}else{
			$data['uid'] = $user[uid];
			$data['qq'] = $uin;
			$data['sid'] = $sid;
			$data['skey'] = $skey;
			$data['sidzt'] = 0;
			$data['addtime'] = date("Y-m-d H:i:s");
			if(M("qqs")->data($data)->add()){
				$row=M("qqs")->field('qid')->where("qq='$uin' and uid='$user[uid]'")->find();
				exit('{"saveOK":0,"msg":"添加成功","url":"'.U('set').'&qid='.$row[qid].'"}');
			}else{
				exit('{"saveOK":-1,"msg":"保存数据库失败"}');
			}
		}
	
	}
	public function login(){
		$uin=empty($_POST['uin'])?exit('{"saveOK":-1,"msg":"uin不能为空"}'):$_POST['uin'];
		$p=empty($_POST['p'])?exit('{"saveOK":-1,"msg":"p不能为空"}'):$_POST['p'];
		$vcode=empty($_POST['vcode'])?exit('{"saveOK":-1,"msg":"vcode不能为空"}'):$_POST['vcode'];
		$pt_verifysession=empty($_POST['pt_verifysession'])?exit('{"saveOK":-1,"msg":"pt_verifysession不能为空"}'):$_POST['pt_verifysession'];

		$url='http://ptlogin2.qzone.com/login?pt_vcode_v1=0&pt_verifysession_v1='.$pt_verifysession.'&verifycode='.$vcode.'&u='.$uin.'&p='.$p.'&pt_randsalt=0&ptlang=2052&low_login_enable=0&u1=http%3A%2F%2Fz.qq.com%2Findex.jsp&from_ui=1&fp=loginerroralert&device=2&aid=549000929&pt_ttype=1&ptredirect=1&h=1&g=1&pt_uistyle=9&';
		$ret = $this->get_curl($url,0,0,0,1);
		if(preg_match("/ptuiCB\('(.*?)'\);/", $ret, $arr)){
			$r=explode("','",$arr[1]);
			if($r[0]==0){
				preg_match('/skey=@(.{9});/',$ret,$skey);
				if($sid=$this->getsid($r[2])){
					exit('{"saveOK":0,"uin":"'.$uin.'","sid":"'.$sid.'","skey":"@'.$skey[1].'"}');
				}else{
					exit('{"saveOK":-3,"msg":"登陆成功，获取SID失败！"}');
				}
			}elseif($r[0]==4){
				exit('{"saveOK":4,"uin":"'.$uin.'","msg":"验证码错误"}');
			}elseif($r[0]==3){
				exit('{"saveOK":3,"uin":"'.$uin.'","msg":"密码错误"}');
			}elseif($r[0]==19){
				exit('{"saveOK":19,"uin":"'.$uin.'","msg":"您的帐号暂时无法登录，请到 http://aq.qq.com/007 恢复正常使用"}');
			}else{
				exit('{"saveOK":-5,"msg":"'.$r[4].'"}');
			}
		}else{
			exit('{"saveOK":-2,"msg":"'.$ret.'"}');
		}
	}
	public function getvc(){
		if(!$uin=$_POST['uin']) exit('{"saveOK":-1,"msg":"请先输入QQ号码"}');
		if(!preg_match("/^[1-9][0-9]{4,11}$/",$uin)) exit('{"saveOK":-2,"msg":"QQ号码不正确"}');
		$url='http://captcha.qq.com/getQueSig?aid=549000929&uin='.$uin.'&captype=3&0.360241'.time();
		$data=$this->get_curl($url);
		if(preg_match("/cap_getCapBySig\(\"(.*?)\"\);/", $data, $arr)){
			exit('{"saveOK":0,"vc":"'.$arr[1].'"}');
		}else{
			exit('{"saveOK":-3,"msg":"获取验证码失败"}');
		}
	}
	public function checkvc(){
		if(!$uin=$_POST['uin']) exit('{"saveOK":-1,"msg":"请先输入QQ号码"}');
		if(!preg_match("/^[1-9][0-9]{4,11}$/",$uin)) exit('{"saveOK":-2,"msg":"QQ号码不正确"}');
		$url='http://check.ptlogin2.qzone.com/check?pt_tea=1&uin='.$uin.'&appid=549000929&ptlang=2052&r=0.9947'.time();
		$data=$this->get_curl($url);
		/*
		if(preg_match_all('/Set-Cookie:(.*);/iU', $data, $cookie)){//有cookies返回
			foreach($cookie[1] as $value){
				$tmp=explode("=",$val);
				$cookie_array[$tmp[0]]=$tmp[1];
			}
		}*/
		if(preg_match("/ptui_checkVC\('(.*?)'\);/", $data, $arr)){
			$r=explode("','",$arr[1]);
			if(strlen($r[1])>4){
				$jsondata=array();
				$jsondata[]=1;$jsondata[]=$uin;$jsondata[]=$r[2];
				$json[saveOK]=0;
				$json[data]=$jsondata;
				exit('{"saveOK":0,"data":"1,'.$uin.'"}');
				exit('1,'.$uin.','.$r[2]);
			}else{
				setcookie("qqlogin_vcode",$r[2]);
				exit('{"saveOK":0,"data":"'.$r[0].','.$uin.','.$r[1].','.$r[3].'"}');
			}
		}else{
			exit('{"saveOK":-3,"msg":"获取验证码失败"}');
		}
	}

	private function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);//主要头部
			//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跟随重定向
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	
	}
	private function getsid($url){		
		$ret = $this->get_curl($url,0,0,0,1,0,1);
		if(preg_match("/Location:(.*)\n/iU", $ret,$u)){
			if(preg_match('/sid=(.{24})/',$u[1],$sid)){
				return $sid[1];
			}else{
				return;
			}
		}else{
			return;
		}
	}
}

$login=new qqlogin();
if($_GET['do']=='checkvc'){
	$login->checkvc();
}
if($_GET['do']=='getvc'){
	$login->getvc();
}
if($_GET['do']=='login'){
	$login->login();
}
if($_GET['do']=='getvcpic'){
	$login->getvcpic();
}

